// pages/record/record.js
const config = require('../../config.js')
const http = require('../../http.js')
const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 1,
    isPlay:false,
    roomId:0,
    itemId:0,
    tmpfile: {
      tempFilePath: '',
      duration: 0,
      fileSize: 0
    },
    countDownNum: 0, //计时初始值
    countDownValue: "00:00",
    lawItem:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options==", options)
    this.setData({
      roomId:options.roomId,
      itemId:options.itemId
    })
    let value = wx.getStorageSync('lawItem')
    if(value){
      this.setData({
        lawItem:value
      })
    }
    this.initRecorderManager()
    this.initInnerAudioContext()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("onHide")
    this.stopRecord()
    if(this.data.isPlay){
      this.stopVoice()
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("onUnload")
    this.stopRecord()
    if(this.data.isPlay){
      this.stopVoice()
    }
  },

  initRecorderManager: function () {
    var that = this
    recorderManager.onStart((res) => {
      console.log("onStart", res)
    })
    recorderManager.onPause((res) => {
      console.log("onPause", res)
    })
    recorderManager.onResume((res) => {
      console.log("onResume", res)
    })
    recorderManager.onStop((res) => {
      console.log("onStop", res)
      this.setData({
        tmpfile: res
      })
      innerAudioContext.src=res.tempFilePath
    })
    recorderManager.onError((res) => {
      console.log("onError", res)
    })
    recorderManager.onInterruptionBegin((res) => {
      console.log("onInterruptionBegin", res)
      that.stopRecord()
    })
    recorderManager.onInterruptionEnd((res) => {
      console.log("onInterruptionEnd", res)
    })
  },

  initInnerAudioContext: function () {
    innerAudioContext.onPlay((res) => {
      console.log("onPlay", res)
    })
    innerAudioContext.onPause((res) => {
      console.log("onPause", res)
    })
    innerAudioContext.onError((res) => {
      console.log("onError", res)
    })
    innerAudioContext.onSeeked((res) => {
      console.log("onSeeked", res)
    })
    innerAudioContext.onStop((res) => {
      console.log("onStop", res)
    })
    innerAudioContext.onWaiting((res) => {
      console.log("onWaiting", res)
    })
    innerAudioContext.onEnded((res) => {
      console.log("onEnded", res)
      this.setData({
        isPlay:false
      })
      clearInterval(this.data.timer); 
    })
  },
/**
   * 播放
   */
  playVoice: function () {
    innerAudioContext.play()
    console.log("播放")
    this.setData({
      isPlay:true
    })
    this.setData({
      countDownNum: 0, //计时初始值
      countDownValue: "00:00",
    })
    this.countDown();
  },

   /**
   * 停止
   */
  stopVoice: function () {
    innerAudioContext.stop()
    this.setData({
      isPlay:false
    })
    clearInterval(this.data.timer); 
  },


  /**
   * 开始录音
   */
  startRecord: function () {
    this.setData({
      countDownNum: 0, //计时初始值
      countDownValue: "00:00",
    })
    this.countDown();
    const options = {
      duration: 600000,
      format: 'mp3',
    }
    recorderManager.start(options)
    this.setData({
      step: 2,
    })
  },
  /**
   * 暂停录音
   */
  pauseRecord: function () {
    recorderManager.pause()
  },
  /**
   * 继续录音
   */
  resumeRecord: function () {
    recorderManager.resume()
  },
  /**
   * 结束录音
   */
  stopRecord: function () {
    recorderManager.stop()
    this.setData({
      step: 3,
    })
    clearInterval(this.data.timer); 
    this.setData({
      countDownNum: 0, //计时初始值
      countDownValue: "00:00",
    })
  },
  resetRecord: function () {
    this.setData({
      step: 1,
    })
    this.setData({
      countDownNum: 0, //计时初始值
      countDownValue: "00:00",
    })
  },
  uploadRecord: function () {
    console.log("uploadRecord")
    wx.showLoading({
      title: '正在上传音频文件',
    })
    wx.uploadFile({
      filePath: this.data.tmpfile.tempFilePath,
      name: 'file',
      url: config.finished,
      formData: {
        'itemId': this.data.itemId,
        'meetingId':this.data.roomId
      },
      header: {
        'Cookie': 'sessionid=' + wx.getStorageSync('sessionId')
      },
      success(res){
        wx.showToast({
          title: '文件上传成功',
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1500);
      },
      fail(err){
        console.log("err==",err)
      },
      complete(e){
        wx.hideLoading()
       
      }
    })
  },

  
  countDown: function () {
    let that = this;
    let second = that.data.countDownNum; //获取计时初始值
    that.data.timer = setInterval(function () {
      // console.log("second==", second)
      if(second >= 600){
        wx.showToast({
          title: '录音时长已到最大值',
          icon: 'none'
        })
        that.setData({
          countDownNum: second,
        })
        that.stopRecord()
      } else {
        second++;
        let m = Math.floor(second / 60)
        let s = second % 60
        m += ''
        s += ''
        m = (m.length == 1) ? '0' + m : m;
        s = (s.length == 1) ? '0' + s : s;
        that.setData({
          countDownNum: second,
          countDownValue: m + ':' + s
        })
      }
    }, 1000)
  },

})